extends Area2D
# 子弹移动速度
const SPEED = 5

signal onTankDie(tank)

var from_body : KinematicBody2D

func _ready():
	$Sprite.animation = "normal"
	$Sprite.play()
	pass

func _physics_process(delta):
	var length = position.length()
	# 只有在位置大于0，并且动画为子弹模式下，才可以进行位移
	if length > 0 and $Sprite.animation == "normal":
		if rotation_degrees == -90:
			position.x += -SPEED
			pass
		elif rotation_degrees == 0:
			position.y += -SPEED
			pass
		elif rotation_degrees == 90:
			position.x += SPEED
			pass
		elif rotation_degrees == 180:
			position.y += SPEED
			pass
	pass

func start(position:Vector2,rotation:float,from_body:KinematicBody2D):
	self.position = position
	self.rotation_degrees = rotation
	self.from_body = from_body
	pass


func _on_Bullet_body_entered(body):
	
	# 子弹触碰到的不是自己人，那就是敌人，或者墙壁，那就发生爆炸
	if body.get_groups() != from_body.get_groups():
		$Sprite.animation = "boom"
		if body.is_in_group("tank"):
			body.queue_free()
			emit_signal("onTankDie",body)
		pass
	pass


# 动画结束
func _on_Sprite_animation_finished():
	# 爆炸动画一结束，就销毁自身
	if $Sprite.animation == "boom":
		$Sprite.stop()
		queue_free()
		pass
	pass 
